home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / dev / e / jrhrkrm2.lzh / RKRM_PartTwo / IFFParse / clipftxt.e < prev    next >
Text File  |  1995-09-20  |  5KB  |  167 lines

  1. -> clipftxt.e - Writes ASCII text to clipboard unit as FTXT
  2. ->              (All clipboard data must be IFF)
  3. ->
  4. -> Usage: clipftxt unitnumber
  5. ->
  6. -> To convert to an example of reading only, comment out #define WRITEREAD
  7.  
  8. ->>> Header (globals)
  9. OPT PREPROCESS
  10.  
  11. MODULE 'iffparse',
  12.        'libraries/iffparse',
  13.        'other/split'
  14.  
  15. ENUM ERR_NONE, ERR_ARGS, ERR_CLIP, ERR_IFF, ERR_LIB, ERR_OIFF, ERR_STOP,
  16.      ERR_USE, ERR_WRIT
  17.  
  18. RAISE ERR_CLIP IF OpenClipboard()=NIL,
  19.       ERR_IFF  IF AllocIFF()=NIL,
  20.       ERR_LIB  IF OpenLibrary()=NIL,
  21.       ERR_OIFF IF OpenIFF()<>0,
  22.       ERR_STOP IF StopChunk()<>FALSE
  23.  
  24. -> Causes example to write FTXT first, then read it back.
  25. -> Comment out to create a reader only
  26. #define WRITEREAD
  27.  
  28. -> E-Note: using argSplit() so one arg count is one less than C's argv
  29. CONST MINARGS=1, RBUFSZ=512, ID_FTXT="FTXT", ID_CHRS="CHRS"
  30.  
  31. DEF usage, errormsgs:PTR TO LONG, mytext
  32. ->>>
  33.  
  34. ->>> PROC main()
  35. PROC main() HANDLE
  36.   DEF iff=NIL:PTR TO iffhandle, cn:PTR TO contextnode, error=0, unitnumber=0,
  37.       rlen, textlen, readbuf[RBUFSZ]:ARRAY, arglist:PTR TO LONG, going=TRUE
  38.   -> E-Note: set-up globals
  39.   usage:='Usage: clipftxt unitnumber (use zero for primary unit)\n'
  40.   -> Text error messages for possible IFFERR_#? returns from various IFF
  41.   -> routines.  To get the index into this array, take your IFFERR code,
  42.   -> negate it, and subtract one.
  43.   ->  idx = -error - 1;
  44.   errormsgs:=['End of file (not an error).', 'End of context (not an error).',
  45.               'No lexical scope.', 'Insufficient memory.',
  46.               'Stream read error.', 'Stream write error.',
  47.               'Stream seek error.', 'File is corrupt.', 'IFF syntax error.',
  48.               'Not an IFF file.', 'Required call-back hook missing.',
  49.               'Return to client.  You should never see this.']:LONG
  50.   mytext:='This FTXT written to clipboard by clipftxt example.\n'
  51.   textlen:=STRLEN
  52.   IF NIL=(arglist:=argSplit()) THEN Raise(ERR_ARGS)
  53.   -> If not enough args or "?", print usage
  54.   IF ListLen(arglist)<>MINARGS THEN Raise(ERR_USE)
  55.   IF arglist[][]="?" THEN Raise(ERR_USE)
  56.   unitnumber:=Val(arglist[])
  57.  
  58.   iffparsebase:=OpenLibrary('iffparse.library', 0)
  59.  
  60.   -> Allocate IFF_File OBJECT
  61.   iff:=AllocIFF()
  62.  
  63.   -> Set up IFF_File for Clipboard I/O.
  64.   iff.stream:=OpenClipboard(unitnumber)
  65.   InitIFFasClip(iff)
  66.   WriteF('Opened clipboard unit \d\n', unitnumber)
  67.  
  68.   InitIFFasClip(iff)
  69.  
  70. #ifdef WRITEREAD
  71.  
  72.   -> Start the IFF transaction.
  73.   OpenIFF(iff, IFFF_WRITE)
  74.                 
  75.   -> Write our text to the clipboard as CHRS chunk in FORM FTXT
  76.   ->
  77.   -> First, write the FORM ID (FTXT)
  78.   IF FALSE=(error:=PushChunk(iff, ID_FTXT, ID_FORM, IFFSIZE_UNKNOWN))
  79.     -> Now the CHRS chunk ID followed by the chunk data.  We'll just write one
  80.     -> CHRS chunk.  You could write more chunks.
  81.     IF FALSE=(error:=PushChunk(iff, 0, ID_CHRS, IFFSIZE_UNKNOWN))
  82.       -> Now the actual data (the text)
  83.       IF WriteChunkBytes(iff, mytext, textlen)<>textlen
  84.         WriteF('Error writing CHRS data.\n')
  85.         error:=IFFERR_WRITE
  86.       ENDIF
  87.     ENDIF
  88.     IF FALSE=error THEN error:=PopChunk(iff)
  89.   ENDIF
  90.   IF FALSE=error THEN error:=PopChunk(iff)
  91.  
  92.   IF error THEN Raise(ERR_WRIT)
  93.  
  94.   WriteF('Wrote text to clipboard as FTXT\n')
  95.   
  96.   -> Now let's close it, then read it back.  First close the write handle, then
  97.   -> close the clipboard.
  98.   CloseIFF(iff)
  99.   IF iff.stream THEN CloseClipboard(iff.stream)
  100.   iff.stream:=NIL  -> E-Note: reinitialise it to NIL to help error trapping
  101.  
  102.   iff.stream:=OpenClipboard(unitnumber)
  103.   WriteF('Reopened clipboard unit \d\n', unitnumber)
  104.  
  105. #endif -> WRITEREAD
  106.  
  107.   OpenIFF(iff, IFFF_READ)
  108.                 
  109.   -> Tell iffparse we want to stop on FTXT CHRS chunks
  110.   StopChunk(iff, ID_FTXT, ID_CHRS)
  111.                 
  112.   -> Find all of the FTXT CHRS chunks
  113.   -> E-Note: the going flag makes this easier to understand
  114.   WHILE going
  115.     error:=ParseIFF(iff, IFFPARSE_SCAN)
  116.     IF error=IFFERR_EOC  -> Enter next context
  117.     ELSEIF error
  118.       going:=FALSE
  119.     ELSE
  120.       -> We only asked to stop at FTXT CHRS chunks.  If no error we've hit a
  121.       -> stop chunk.  Read the CHRS chunk data
  122.       cn:=CurrentChunk(iff)
  123.  
  124.       IF cn
  125.         IF (cn.type=ID_FTXT) AND (cn.id=ID_CHRS)
  126.           WriteF('CHRS chunk contains:\n')
  127.           WHILE (rlen:=ReadChunkBytes(iff, readbuf, RBUFSZ)) > 0
  128.             -> E-Note: stdout is safe since WriteF() has been used above
  129.             Write(stdout, readbuf, rlen)
  130.           ENDWHILE
  131.           IF rlen<0 THEN error:=rlen
  132.         ENDIF
  133.       ENDIF
  134.     ENDIF
  135.   ENDWHILE
  136.  
  137.   IF error AND (error<>IFFERR_EOF)
  138.     WriteF('IFF read failed, error \d: \s\n', error, errormsgs[-error-1])
  139.   ENDIF
  140. EXCEPT DO
  141.   IF iff
  142.     -> Terminate the IFF transaction with the stream.  Free all associated
  143.     -> structures.
  144.     CloseIFF(iff)
  145.     -> Close the clipboard stream
  146.     IF iff.stream THEN CloseClipboard(iff.stream)
  147.     -> Free the IFF_File structure itself.
  148.     FreeIFF(iff)
  149.   ENDIF
  150.   IF iffparsebase THEN CloseLibrary(iffparsebase)
  151.   SELECT exception
  152.   CASE ERR_CLIP;  WriteF('Error: could not open clipboard\n')
  153.   CASE ERR_IFF;   WriteF('Error: could not allocate IFF handle\n')
  154.   CASE ERR_LIB;   WriteF('Error: could not open iffparse.library\n')
  155.   CASE ERR_OIFF;  WriteF('Error: could not open IFF handle\n')
  156.   CASE ERR_USE;   WriteF(usage)
  157.   CASE ERR_WRIT;  WriteF('IFF write failed, error \d: \s\n', error, errormsgs[-error-1])
  158.   ENDSELECT
  159. ENDPROC
  160. ->>>
  161.  
  162. ->>> Version string
  163. -> 2.0 Version string for c:Version to find
  164. vers:
  165.   CHAR 0, '$VER: clipftxt 37.2', 0
  166. ->>>
  167.